<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace Modules\System\Services;

use Modules\System\Models\Tasks;
use Illuminate\Support\Facades\Storage;

class TasksService {

	public function getDataList($request){
		$map = [];

		if ($request->filled('title')) {
			$map[] = ['title', 'like', '%' . $request->input('title') . '%'];
		}
		if ($request->filled('name')) {
			$map[] = ['name', 'like', '%' . $request->input('name') . '%'];
		}
		if ($request->filled('url')) {
			$map[] = ['url', 'like', '%' . $request->input('url') . '%'];
		}
		if ($request->filled('method')) {
			$map[] = ['method', 'like', '%' . $request->input('method') . '%'];
		}
		if ($request->filled('ip')) {
			$map[] = ['ip', 'like', '%' . $request->input('ip') . '%'];
		}
		if ($request->filled('user_id')) {
			$map[] = ['user_id', '=', $request->input('user_id')];
		}
		if ($request->filled('data')) {
			$map[] = ['data', 'like', '%' . $request->input('data') . '%'];
		}
		if ($request->filled('remark')) {
			$map[] = ['remark', 'like', '%' . $request->input('remark') . '%'];
		}
		if ($request->filled('status')) {
			$map[] = ['status', '=', $request->input('status')];
		}
		if ($request->filled('is_my')) {
			$user = auth('admin')->user();
			$map[] = ['user_id', '=', $user->uid];
		}

		$query = Tasks::with(['user:nickname,username,uid'])->where($map)->orderBy('id', 'desc');

		if ($request->filled('page')) {
			$data = [
				'total' => $query->count(),
				'page' => $request->input('page', 1),
				'data' => $query->offset($request->input('offset', 0))->limit($request->input('limit', 10))->get()->append(['url']),
			];
		}else{
			$data = $query->get()->append(['url']);
		}
		return $data;
	}

	public function create($data){
		$task = new Tasks();

		$task->type = $data['type'] ? $data['type'] : 'export';
		$task->title = $data['title'] ? $data['title'] : $task->type . '任务';
		$task->file = $data['file'] ? $data['file'] : '';
		$task->status = $data['status'] ? $data['status'] : 0;
		$task->user_id = auth('admin')->user()->uid;

		$task->save();
		return $task;
	}

	public function update($data){
		$task = Tasks::where('id', $data['id'])->first();

		if ($data['status'] != '') {
			$task->status = $data['status'];
		}
		if ($data['file'] != '') {
			$task->file = $data['file'];
		}
		if (in_array($data['type'], ['import', 'export'])) {
			$task->type = $data['type'];
		}
		if ($data['title'] != '') {
			$task->title = $data['title'];
		}

		$task->save();
	}

	public function delete($data){
		$task = Tasks::where('id', $data['id'])->first();

		if ($task->status == 0) {
			throw new \Exception("该任务正在执行中，无法删除");
		}

		if ($task->file) {
			Storage::disk('public')->delete($task->file);
		}

		$task->delete();
		return $task;
	}
}